package com.second.app.thread.volatiled;

import com.second.app.thread.join.sleep_unreleaselock.A;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author GYZ
 * <pre>
 *
 * </pre>
 */
public class VolatileDemo {
    public static  volatile   boolean notice = false;

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();

        Thread a = new Thread(() -> {

            for (int i = 0; i <10; i++) {
                list.add("abc");
                System.out.println("线程:"+Thread.currentThread().getName()+"向list中添加一个元素，此时list集合size为："+list.size());
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(list.size()==5){
                    notice = true;
                }
            }

        }, "线程A");


        Thread b = new Thread(() -> {

            while (true){
                if(notice){
                    System.out.println("线程："+Thread.currentThread().getName()+"，接到通知，执行自己的业务");
                }
            }

        }, "线程B");

        b.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a.start();
    }
}
